<template>
  <div class="production_feed">
    <div class="btn_panel">
      <mes-search-comp
        ref="mesSearch2"
        :searchList="searchList"
        :searchResult="searchResult"
        :searchSeniorShow="false"
        @searchInfo="_info"
        @exportTable="_exportTable">
      </mes-search-comp>
    </div>
    <div class="table_container">
      <el-table
        ref="tableNode"
        :data="listData.list"
        border
        stripe
        style="width: 100%"
        :max-height="tableCascadeHeight"
        @sort-change="_sort"
        :row-class-name="({row}) => !selectData.some((item) => item.id === row.id) || 'line_selected'"
        @row-click="(row) => $refs.tableNode.toggleRowSelection(row)"
        @selection-change="handleSelectionChange"
        @filter-change="handleFilterChange"
        v-loading="isLoading">
        <el-table-column
          fixed="left"
          align="center"
          type="selection"
          width="40">
        </el-table-column>
        <mes-col-comp
          :key="index"
          v-for="(column, index) in columnList"
          :column="column">
        </mes-col-comp>
        <el-table-column
          fixed="right"
          :resizable="false"
          prop="id"
          label="操作"
          align="center"
          min-width="80"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <span class="jump_item" @click.stop="checkRequiredDetails(scope.row.id)">要料明细</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <required-detail ref="RequiredDetail"></required-detail>
    <mes-pagination
      @updateData="init"
      :currentPage="listData.pageNo"
      :total="listData.count || 0"
      :pageSize="listData.pageSize">
    </mes-pagination>
  </div>
</template>

<script>
import computed from './productionFeedCol'
import RequiredDetail from './components/RequiredDetail'
export default {
  name: 'ProductionFeed',
  props: {
    ids: String
  },
  data() {
    return {
      isLoading: false, // 是否加载
      showStatus: false, // 组件是否显示
      searchData: {}, // 搜索列表数据
      sortData: {}, // 排序字段
      insertData: {}, // 调度组件数据导入
      searchResult: NaN, // 搜索结果数
      selectData: [], // 清单选择列表
      listData: [], // 清单列表数据
      workorderIds: '', // 工单ID
      currentIds: '', // 当前请求的工单id
      codeValue: '', // 扫码数值
      workeroderMsg: null, // 任务号、工单号
    }
  },
  components: {
    RequiredDetail
  },
  computed: computed,
  mounted() {
    this.workorderIds = this.ids
    this.init()
  },
  methods: {
    // 列筛选回调
    handleFilterChange(filters) {
      this.sortData = {}
      this.$refs.tableNode.clearSort()
      let filterParams = this.$filterParams(this.searchList, filters)
      Object.assign(this.searchData, filterParams.search)
      Object.assign(this.sortData, filterParams.order)
      this.init()
    },
    handleCodeScan(value) {
      this.$refs.mesSearch2.clean()
      this.codeValue = value
      this.init()
    },
    // 导出表格
    _exportTable() {
      let postData = this.getPostData({
        pageNo: 1,
        pageSize: 9999999
      })
      this.$exportTable(postData, 'a/materialrequest/materialRequest/exportasexcel', '待审核生产补料')
    },
    /**
     * 刷新数据
     * @param ids 工单id
     * @param isClean 是否清空搜索条件
     *  @param workeroderMsg 任务号、工单号
     */
    refresh(ids, isClean, workeroderMsg) {
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.sortData = {}
      this.workorderIds = ids || (Array.isArray(workeroderMsg) ? workeroderMsg.join(',') : '')
      if (!Array.isArray(workeroderMsg) && workeroderMsg) {
        this.workeroderMsg = {taskNumber: workeroderMsg || undefined}
      } else {
        this.workeroderMsg = {taskNumber: undefined}
      }
      if (isClean) {
        this.searchData = {}
        this.$refs.mesSearch2.clean()
      }
      if (!this.isLoading) this.init()
    },
    // 获取请求数据
    getPostData(page) {
      let postData = {
        pageNo: page ? page.pageNo : 1,
        pageSize: page ? page.pageSize : this.listData.pageSize || 15,
        sort: this.sortData,
        query: {
          ...this.searchData,
          ...this.workeroderMsg,
          ...{
            requestStatusId: this.toBeApprovedId,
            requestTypeId: this.supplyId,
            viewSource: 3,
          }
        }
      }
      if (this.workorderIds) {
        postData.query.workorderId = this.workorderIds
        this.currentIds = this.workorderIds
      } else {
        this.currentIds = ''
      }
      if (this.codeValue) {
        postData.query.number = this.codeValue
      }
      return postData
    },
    /**
     * 列表数据请求
     * @param page
     */
    init(page) {
      this.isLoading = true
      let postData = this.getPostData(page)
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/materialrequest/materialRequest/list',
        data: postData,
        noCommit: true
      }).then((data) => {
        let res = data.data
        this.listData = res
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
        this.$emit('getFeedList', res.count)
      }).finally(() => {
        if (this.currentIds !== this.workorderIds) {
          this.init()
        }
        this.isLoading = false
      })
    },
    // 条件查询
    _info(data) {
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.sortData = {}
      this.searchData = data
      this.init()
    },
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    /**
     * 选择表格
     * @param val
     */
    handleSelectionChange(val) {
      this.selectData = val
      this.$emit('selectFeedList', this.selectData)
    },
    // 查看要料明细
    checkRequiredDetails(id) {
      this.$refs.RequiredDetail.openDialog(id)
    },
    // 补料申请审批
    applyHandle(value) {
      let str = ''
      if (value === 'Refused') str = '退回'
      if (value === 'Request') str = '通过'
      this.applyHandleResquest(str, value)
    },
    // 撤销
    applyHandleResquest(str, value) {
      this.mesConfirm(`是否${str}该补料申请?`, ` ${str}提示`).then(() => {
        const ids = this.selectData.map(item => item.id).join(',')
        const statusId = this.materialRequestStatus.find(item => item.value === value).id
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/materialrequest/materialRequest/changestatus',
          data: { ids: ids, statusId: statusId },
          noCommit: true
        }).then(() => {
          this.init()
          this.tipMessage('success', `成功${str}`)
        })
      })
    },
    /**
     * 提示
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>

<style lang="less">
  .production_feed{
    .mes_search{
      width: 100%;
      .search_ordinary{
        padding-right: 0;
        margin-right: 0;
      }
    }
  }
</style>
